﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;

namespace LetiLib.Connection
{
    public class Receiver
    {
        public static bool bAcceptCmdHeader(byte[] btReceive)
        {
            if (btReceive.Length <= 0)
                return false;
            //check first byte
            if (btReceive[0] != (byte)COMMAND.DEVICE_TAG)
                return false;
            return true;
        }

        public static int bAcceptPackageHeader(byte[] btReceive)
        {
            int ret = -1;

            for (int i = 0; i < btReceive.Length; i++)
            {

                if (btReceive[i] == (byte)COMMAND.DEVICE_TAG)// && btReceive[i + 1] == DEFS.CMD_BLACKBOX_DATALOG)
                {
                    ret = i;
                    break;
                }
            }

            return ret;
        }
        public static bool bAcceptVersionCmd(byte[] btReceive, byte bClientType)
        {
            if (btReceive.Length != 5 || btReceive[0] != bClientType)
                return false;
            //check if btReceive[4] is an integer
            try
            {
                Int16.Parse(btReceive[4].ToString());
                return true;
            }
            catch
            {
                return false;
            }
        }

        // tungpt added >>
        public static bool bAcceptPlaylistReqCmd(byte[] btReceive)
        {
            if (btReceive[0] == (byte)COMMAND.DEVICE_TAG &&
                btReceive[1] == (byte)COMMAND.CMD_SERVER_PLAYLIST_UPDATE_REQ &&
                btReceive[2] == (byte)DEVICE_RESPONSE.SUCCESS)
                return true;
            else return false;
        }
        public static bool bAcceptPlaylistInfo(byte[] btReceive)
        {
            if (btReceive[0] == (byte)COMMAND.DEVICE_TAG &&
                btReceive[1] == (byte)COMMAND.PLAYLIST_INFO_HEADER &&
                btReceive[2] == (byte)DEVICE_RESPONSE.SUCCESS)
                return true;
            else return false;
        }
        public static bool bAcceptPlaylistError(byte[] btReceive)
        {
            if (btReceive[0] == (byte)COMMAND.DEVICE_TAG &&
                btReceive[1] == (byte)COMMAND.PLAYLIST_INFO_HEADER &&
                btReceive[2] >= (byte)DEVICE_RESPONSE.ERROR)
                return true;
            else return false;
        }
        public static bool bAcceptDataReqCmd(byte[] btReceive)
        {
            if (btReceive.Length >= 3 && btReceive[0] == (byte)COMMAND.DEVICE_TAG &&
                btReceive[1] == (byte)COMMAND.CMD_SERVER_DATA_UPDATE_REQ &&
                btReceive[2] == (byte)DEVICE_RESPONSE.SUCCESS)
                return true;
            else return false;
        }
        public static bool bAcceptDataInfo(byte[] btReceive)
        {
            if (btReceive[0] == (byte)COMMAND.DEVICE_TAG &&
                btReceive[1] == (byte)COMMAND.DATA_INFO_HEADER &&
                btReceive[2] == (byte)DEVICE_RESPONSE.SUCCESS)
                return true;
            else return false;
        }

        public static bool bAcceptDataError(byte[] btReceive)
        {
            if (btReceive[0] == (byte)COMMAND.DEVICE_TAG &&
                btReceive[1] == (byte)COMMAND.DATA_INFO_HEADER &&
                btReceive[2] >= (byte)DEVICE_RESPONSE.ERROR)
                return true;
            else return false;
        }

        public static bool bAcceptDataPackage(byte[] btReceive)
        {
            if (btReceive[0] == (byte)COMMAND.DEVICE_TAG &&
                btReceive[1] == (byte)COMMAND.DATA_PACKAGE_HEADER &&
                btReceive[2] == (byte)DEVICE_RESPONSE.SUCCESS)
                return true;
            else return false;
        }
        public static bool bAcceptDataPackageIDError(byte[] btReceive)
        {
            if (btReceive[0] == (byte)COMMAND.DEVICE_TAG &&
                btReceive[1] == (byte)COMMAND.DATA_PACKAGE_HEADER &&
                btReceive[2] == (byte)DEVICE_RESPONSE.ERROR_PACKAGE_ID)
                return true;
            else return false;
        }
        public static bool bAcceptDataPackageError(byte[] btReceive)
        {
            if (btReceive[0] == (byte)COMMAND.DEVICE_TAG &&
                btReceive[1] == (byte)COMMAND.DATA_PACKAGE_HEADER &&
                btReceive[2] == (byte)DEVICE_RESPONSE.ERROR_PACKAGE_CHECKSUM)
                return true;
            else return false;
        }

        public static bool bAcceptFirmwareReqCmd(byte[] btReceive)
        {
            if (btReceive[0] == (byte)COMMAND.DEVICE_TAG &&
                btReceive[1] == (byte)COMMAND.CMD_SERVER_FIRMWARE_UPDATE_REQ &&
                btReceive[2] == (byte)DEVICE_RESPONSE.SUCCESS)
                return true;
            else return false;
        }
        public static bool bAcceptFirmwareInfo(byte[] btReceive)
        {
            if (btReceive[0] == (byte)COMMAND.DEVICE_TAG &&
                btReceive[1] == (byte)COMMAND.FIRMWARE_INFO_HEADER &&
                btReceive[2] == (byte)DEVICE_RESPONSE.SUCCESS)
                return true;
            else return false;
        }

        public static bool bAcceptFirmwareError(byte[] btReceive)
        {
            if (btReceive[0] == (byte)COMMAND.DEVICE_TAG &&
                btReceive[1] == (byte)COMMAND.FIRMWARE_INFO_HEADER &&
                btReceive[2] == (byte)DEVICE_RESPONSE.ERROR)
                return true;
            else return false;
        }

        public static bool bAcceptFirmwarePackage(byte[] btReceive)
        {
            if (btReceive[0] == (byte)COMMAND.DEVICE_TAG &&
                btReceive[1] == (byte)COMMAND.FIRMWARE_PACKAGE_HEADER &&
                btReceive[2] == (byte)DEVICE_RESPONSE.SUCCESS)
                return true;
            else return false;
        }
        public static bool bAcceptFirmwarePackageIDError(byte[] btReceive)
        {
            if (btReceive[0] == (byte)COMMAND.DEVICE_TAG &&
                btReceive[1] == (byte)COMMAND.FIRMWARE_PACKAGE_HEADER &&
                btReceive[2] == (byte)DEVICE_RESPONSE.ERROR_PACKAGE_ID)
                return true;
            else return false;
        }
        public static bool bAcceptFirmwarePackageError(byte[] btReceive)
        {
            if (btReceive[0] == (byte)COMMAND.DEVICE_TAG &&
                btReceive[1] == (byte)COMMAND.FIRMWARE_PACKAGE_HEADER &&
                btReceive[2] == (byte)DEVICE_RESPONSE.ERROR_PACKAGE_CHECKSUM)
                return true;
            else return false;
        }

        public static bool bAcceptFontReqCmd(byte[] btReceive)
        {
            if (btReceive[0] == (byte)COMMAND.DEVICE_TAG &&
                btReceive[1] == (byte)COMMAND.CMD_SERVER_FONT_UPDATE_REQ &&
                btReceive[2] == (byte)DEVICE_RESPONSE.SUCCESS)
                return true;
            else return false;
        }
        public static bool bAcceptFontInfo(byte[] btReceive)
        {
            if (btReceive[0] == (byte)COMMAND.DEVICE_TAG &&
                btReceive[1] == (byte)COMMAND.FONT_INFO_HEADER &&
                btReceive[2] == (byte)DEVICE_RESPONSE.SUCCESS)
                return true;
            else return false;
        }

        public static bool bAcceptFontPackage(byte[] btReceive)
        {
            if (btReceive[0] == (byte)COMMAND.DEVICE_TAG &&
                btReceive[1] == (byte)COMMAND.FONT_PACKAGE_HEADER &&
                btReceive[2] == (byte)DEVICE_RESPONSE.SUCCESS)
                return true;
            else return false;
        }
        public static bool bAcceptFontPackageIDError(byte[] btReceive)
        {
            if (btReceive[0] == (byte)COMMAND.DEVICE_TAG &&
                btReceive[1] == (byte)COMMAND.FONT_PACKAGE_HEADER &&
                btReceive[2] == (byte)DEVICE_RESPONSE.ERROR_PACKAGE_ID)
                return true;
            else return false;
        }
        public static bool bAcceptFontPackageError(byte[] btReceive)
        {
            if (btReceive[0] == (byte)COMMAND.DEVICE_TAG &&
                btReceive[1] == (byte)COMMAND.FONT_PACKAGE_HEADER &&
                btReceive[2] == (byte)DEVICE_RESPONSE.ERROR)
                return true;
            else return false;
        }

        public static bool bAcceptCheckledInfo(byte[] btReceive)
        {
            if (btReceive[0] == (byte)COMMAND.DEVICE_TAG &&
                btReceive[1] == (byte)COMMAND.LED_INFO_HEADER)
                return true;
            else return false;
        }


        public static bool bAcceptCheckledDataPackage(byte[] btReceive)
        {
            if (btReceive[0] == (byte)COMMAND.DEVICE_TAG &&
                btReceive[1] == (byte)COMMAND.LED_STATUS_DATA)
                return true;
            else return false;
        }
        public static bool bAcceptScreenDataPackage(byte[] btReceive)
        {
            if (btReceive[0] == (byte)COMMAND.DEVICE_TAG &&
                btReceive[1] == (byte)COMMAND.LED_SCREEN_DATA)
                return true;
            else return false;
        }
        // << ended

        //CMDs sent by GPS modules
        public static bool bAcceptIMEI(byte[] btReceive)
        {
            if (btReceive[0] == (byte)COMMAND.DEVICE_TAG &&
                btReceive[1] == (byte)COMMAND.CMD_SEND_IMEI && 
                btReceive.Length == 17)
            {
                return true;
            }
            else return false;
        }

        public static bool bAcceptWarningTooHot(byte[] btReceive)
        {
            if (btReceive[0] == (byte)COMMAND.DEVICE_TAG &&
                btReceive[1] == (byte)COMMAND.WARNING_TOO_HOT && 
                btReceive.Length == 2)
            {
                return true;
            }
            else return false;
        }

        public static bool bAcceptWarningDamaged(byte[] btReceive)
        {
            if (btReceive[0] == (byte)COMMAND.DEVICE_TAG &&
                btReceive[1] == (byte)COMMAND.WARNING_DAMAGED && 
                btReceive.Length == 2)
            {
                return true;
            }
            else return false;
        }

        public static bool bAcceptWarningPowerLost(byte[] btReceive)
        {
            if (btReceive[0] == (byte)COMMAND.DEVICE_TAG &&
                btReceive[1] == (byte)COMMAND.WARNING_OUT_OF_POWER && 
                btReceive.Length == 2)
            {
                return true;
            }
            else return false;
        }

        public static bool bAcceptWarningOpenned(byte[] btReceive)
        {
            if (btReceive[0] == (byte)COMMAND.DEVICE_TAG &&
                btReceive[1] == (byte)COMMAND.WARNING_OPENNED && 
                btReceive.Length == 2)
            {
                return true;
            }
            else return false;
        }

        public static bool bAcceptGetActivePageResponse(byte[] btReceive)
        {
            if (btReceive[0] == (byte)COMMAND.DEVICE_TAG &&
                btReceive[1] == (byte)COMMAND.CMD_GET_ACTIVE_PAGE && 
                btReceive.Length == 4)
            {
                return true;
            }
            else return false;
        }

        public static bool bAcceptGetPageInfoResponse(byte[] btReceive)
        {
            if (btReceive[0] == (byte)COMMAND.DEVICE_TAG &&
               btReceive[1] == (byte)COMMAND.CMD_GET_PAGE_INFO &&
               btReceive.Length > 4)
                return true;
            else return false;
        }

        public static bool bAcceptGetListImageIndexResponse(byte[] btReceive)
        {
            if (btReceive[0] == (byte)COMMAND.DEVICE_TAG &&
               btReceive[1] == (byte)COMMAND.CMD_GET_LIST_IMAGE_INDEX &&
               btReceive.Length > 4)
                return true;
            else return false;
        }
    }
}

